<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/component/pear/css/pear.css" />
    <script src="/static/component/layui/layui.js"></script>
    <script src="/static/component/pear/pear.js"></script>
</head>

<body class="pear-container">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" action="">
                <div class="layui-form-item">

                    <div class="layui-form-item layui-inline">
                        <div class="layui-input-inline">
                            <input type="text" name="title" placeholder="邮件标题" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <div class="layui-input-inline">
                            <input type="text" name="content" placeholder="邮件内容" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <div class="layui-form-item layui-inline">
                            <div class="layui-input-inline">
                                <select name="exastatus" lay-search>
                                    <option value="">审核状态</option>
                                    <option value="aaa">未审核</option>
                                    <option value="bbb">已审核</option>
                                    <option value="ccc">未通过</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" id="exatime-start" name="exatime-start"
                                placeholder="开始时间" autocomplete="off">
                        </div>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" id="exatime-end" name="exatime-end"
                                placeholder="结束时间" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <div class="layui-input-inline">
                            <input type="text" name="touser" placeholder="发送给谁" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <div class="layui-input-inline">
                            <input type="text" name="cmdtxt" placeholder="命令文本" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <div class="layui-input-inline">
                            <select name="exauser" lay-search>
                                <option value="">谁发送的</option>
                                {volist name="userData" id="user"}
                                <option value="{$user.id}">{$user.nickname}</option>
                                {/volist}
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="query">
                            <i class="layui-icon layui-icon-search"></i>
                            查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md">
                            <i class="layui-icon layui-icon-refresh"></i>
                            重置
                        </button>
                    </div>
                </div>

            </form>
        </div>
    </div>
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>

    <div id='layerInfo' class="layui-card" style="display: none;">
        <div class="layui-card-body">
            <form class="layui-form" action="" lay-filter="component-form-element">
                <div class="layui-row layui-col-space10 layui-form-item">
                    <div class="layui-col-lg6">
                        <label class="layui-form-label">标题：</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" placeholder="" autocomplete="off" class="layui-input"
                                readonly>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">申请时间：</label>
                    <div class="layui-input-block">
                        <input type="text" name="exatime" placeholder="" autocomplete="off" class="layui-input"
                            readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">发送类型：</label>
                    <div class="layui-input-block">
                        <input type="radio" name="sendtype" value="1" title="单用户发送" disabled="true">
                        <input type="radio" name="sendtype" value="2" title="单服发送" disabled="true">
                        <input type="radio" name="sendtype" value="3" title="多服发送" disabled="true">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">审核状态：</label>
                    <div class="layui-input-block">
                        <input type="radio" name="exastatus" value="0" title="未审核" disabled="true">
                        <input type="radio" name="exastatus" value="1" title="已审核" disabled="true">
                        <input type="radio" name="exastatus" value="2" title="未通过" disabled="true">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">发送内容：</label>
                    <div class="layui-input-block" style="height: 160px;">
                        <textarea name="content" placeholder="" class="layui-textarea" style="height: 100%;"
                            readonly></textarea>
                    </div>
                </div>

            </form>
        </div>
    </div>

    <script type="text/html" id="toolbar">
            <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="onekeypass">
		        <span class="icon pear-icon">&#xe6c0;</span>
		        一键通过
			</button>
		</script>
    <script type="text/html" id="sendtypeTpl">
            {{# if(d.sendtype == '1'||d.sendtype == '4'||d.sendtype == '5'){ }}
                <button type="button" class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs">单用户</button>
            {{# } else if(d.sendtype == '2'){ }}
                <button type="button" class="layui-btn layui-btn-primary layui-border-orange layui-btn-xs">单区</button>
            {{# } else if(d.sendtype == '3'){ }}
                <button type="button" class="layui-btn layui-btn-primary layui-border-red layui-btn-xs">多区</button>
            {{# } }}
        </script>

    <script type="text/html" id="exastatusTpl">
            {{# if(d.exastatus == '0'){ }}
                <span class="layui-badge layui-bg-orange">未审核</span>
            {{# } else if(d.exastatus == '1'){ }}
                <span class="layui-badge layui-bg-gray">已审核</span>
            {{# } else if(d.exastatus == '2'){ }}
                <span class="layui-badge layui-bg-black">未通过</span>
            {{# } }}
        </script>



    <script type="text/html" id="options">
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="pass"><span class="icon pear-icon">&#xe6ee;</span></button>
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="exit"><span class="icon pear-icon">&#xe679;</span></button>
		</script>

    <script>
        layui.use(['table', 'form', 'jquery', 'common', 'laydate'], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;
            let common = layui.common;
            let laydate = layui.laydate;
            let MODULE_PATH = "{$Request.root}/gm.emailexa/";
            laydate.render({ elem: "#exatime-start" }); laydate.render({ elem: "#exatime-end" });
            let cols = [
                [{
                    type: 'checkbox'
                }, {
                    field: "id",
                    title: "ID",
                    unresize: "true",
                    align: "center"
                }, {
                    field: "sendtype",
                    title: "发送类型",
                    unresize: "true",
                    align: "center",
                    templet: '#sendtypeTpl'
                }, {
                    field: "exastatus",
                    title: "审核状态",
                    unresize: "true",
                    align: "center",
                    templet: '#exastatusTpl'
                }, {
                    field: "title",
                    title: "邮件标题",
                    unresize: "true",
                    align: "center"
                }, {
                    field: "exauser",
                    title: "发送人",
                    unresize: "true",
                    align: "center"
                }, {
                    field: "touser",
                    title: "接收人",
                    unresize: "true",
                    align: "center"
                },
                {
                    field: "content",
                    title: "邮件内容",
                    unresize: "true",
                    align: "center"
                }, {
                    field: "cmdtxt",
                    title: "命令文本",
                    unresize: "true",
                    align: "center"
                }, {
                    field: "exatime",
                    title: "申请时间",
                    unresize: "true",
                    align: "center"
                }, {
                    field: "remake",
                    title: "备注",
                    unresize: "true",
                    align: "center"
                }, {
                    title: '操作',
                    toolbar: '#options',
                    unresize: true,
                    align: 'center',
                    width: 180,
                }
                ]
            ]

            table.render({
                elem: '#dataTable',
                url: MODULE_PATH + 'index',
                page: true,
                cols: cols,
                limit: 10,
                limits: [10, 30, 50, 100, 200, 500, 1000],
                cellMinWidth: 100,
                skin: 'line',
                toolbar: '#toolbar',
                defaultToolbar: [{
                    title: '刷新',
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });

            table.on('rowDouble(dataTable)', function (obj) {
                console.log(obj)
                var rowData = obj.data;
                var primaryKey = rowData.id;

                // 如果不是操作按钮，弹出详细信息弹窗
                layer.open({
                    title: '详细信息',
                    type: 1,
                    shadeClose: true,
                    area: ['700px', '500px'],
                    content: $('#layerInfo')
                });

                // 填充表单内容
                $('#layerInfo input[name="title"]').val(rowData.title);
                $('#layerInfo input[name="exatime"]').val(rowData.exatime);
                $('#layerInfo input[name="sendtype"][value="' + rowData.sendtype + '"]').prop('checked', true);
                $('#layerInfo input[name="exastatus"][value="' + rowData.exastatus + '"]').prop('checked', true);
                $('#layerInfo textarea[name="content"]').val(rowData.content + "\n【" + rowData.cmdtxt + "】");

                // 更新表单渲染
                layui.form.render();
            });

            table.on('tool(dataTable)', function (obj) {
                if (obj.event === 'pass') {
                    window.pass(obj);
                } else if (obj.event === 'exit') {
                    window.exit(obj);
                }
            });

            table.on('toolbar(dataTable)', function (obj) {
                if (obj.event === 'onekeypass') {
                    window.onekeypass(obj);
                }
            });

            form.on('submit(query)', function (data) {
                table.reload('dataTable', {
                    where: data.field,
                    page: { curr: 1 }
                })
                laydate.render({ elem: "#exatime-start" }); laydate.render({ elem: "#exatime-end" });
                return false;
            });

            //弹出窗设置 自己设置弹出百分比
            function screen() {
                if (typeof width !== 'number' || width === 0) {
                    width = $(window).width() * 0.8;
                }
                if (typeof height !== 'number' || height === 0) {
                    height = $(window).height() - 20;
                }
                return [width + 'px', height + 'px'];
            }

            window.pass = function (obj) {
                layer.confirm('确定要通过吗？', { icon: 3, title: '提示' }, function () {
                    if (obj.data['content'] == '福袋') {
                        layer.prompt({ title: '输入福袋数量！', formType: 2 }, function (text, index) {
                            layer.close(index);
                            let loading = layer.load();

                            $.ajax({
                                url: MODULE_PATH + 'pass',
                                data: { id: obj.data['id'], fd: '111', num: text },
                                dataType: 'json',
                                type: 'POST',
                                success: function (res) {
                                    console.log(res)
                                    //判断有没有权限
                                    if (res && res.code == 999) {
                                        layer.msg(res.msg, {
                                            icon: 5,
                                            time: 2000,
                                        })
                                        return false;
                                    } else if (res.code == 200) {
                                        layer.msg(res.msg, {
                                            icon: 1,
                                            time: 1000
                                        }, function () {
                                            table.reload('dataTable');
                                        });
                                    } else {
                                        layer.msg(res.msg, {
                                            icon: 2,
                                            time: 1000
                                        });
                                    }
                                    layer.close(loading);
                                }
                            })
                        });

                    } else {
                        $.ajax({
                            url: MODULE_PATH + 'pass',
                            data: { id: obj.data['id'], fd: '222' },
                            dataType: 'json',
                            type: 'POST',
                            success: function (res) {
                                console.log(res)
                                //判断有没有权限
                                if (res && res.code == 999) {
                                    layer.msg(res.msg, {
                                        icon: 5,
                                        time: 2000,
                                    })
                                    return false;
                                } else if (res.code == 200) {
                                    layer.msg(res.msg, {
                                        icon: 1,
                                        time: 1000
                                    }, function () {
                                        table.reload('dataTable');
                                    });
                                } else {
                                    layer.msg(res.msg, {
                                        icon: 2,
                                        time: 1000
                                    });
                                }
                            }
                        })
                    }


                })
            }

            window.exit = function (obj) {
                layer.confirm('确定要拒绝吗？', { icon: 3, title: '提示' }, function () {
                    $.ajax({
                        url: MODULE_PATH + 'exit',
                        data: { id: obj.data['id'] },
                        dataType: 'json',
                        type: 'POST',
                        success: function (res) {
                            //判断有没有权限
                            if (res && res.code == 999) {
                                layer.msg(res.msg, {
                                    icon: 5,
                                    time: 2000,
                                })
                                return false;
                            } else if (res.code == 200) {
                                layer.msg(res.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function () {
                                    table.reload('dataTable');
                                });
                            } else {
                                layer.msg(res.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })

                })
            }


            window.onekeypass = function (obj) {
                let data = table.checkStatus(obj.config.id).data;
                if (data.length === 0) {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                var ids = []
                var hasCheck = table.checkStatus('dataTable')
                var hasCheckData = hasCheck.data
                if (hasCheckData.length > 0) {
                    $.each(hasCheckData, function (index, element) {
                        ids.push(element.id)
                    })
                }
                layer.confirm('确定要通过这些邮件吗？？？', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    let loading = layer.load();
                    $.ajax({
                        url: MODULE_PATH + 'onekeypass',
                        data: { ids: ids },
                        dataType: 'json',
                        type: 'POST',
                        success: function (res) {
                            console.log(res)
                            layer.close(loading);
                            //判断有没有权限
                            if (res && res.code == 999) {
                                layer.msg(res.msg, {
                                    icon: 5,
                                    time: 2000,
                                })
                                return false;
                            } else if (res.code == 200) {
                                layer.msg(res.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function () {
                                    table.reload('dataTable');
                                });
                            } else {
                                layer.msg(res.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }


        })
    </script>
</body>

</html>